﻿<template>
	<div class="card-fill layout-padding" v-loading="loading" element-loading-text="Loading...">
		<el-card shadow="hover" class="layout-padding-auto create-form">
			<el-form ref="formRef" :model="stateData.vmModel" label-width="100px">
				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>基本信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Phone_FormItem" prop="Entity.Phone">
								<template #label>
									<WtmLabelTooltip label="手机号码" tooltip="接收短信的手机号" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Phone" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div class="form-section">
					<div class="section-title">
						<i class="_wtmicon _wtmicon-wxbzhanghu"></i>
						<span>推送信息</span>
					</div>

					<el-row :gutter="20">
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Context_FormItem" prop="Entity.Context">
								<template #label>
									<WtmLabelTooltip label="推送内容" tooltip="短信推送内容" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Context" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item
								ref="Entity_State_FormItem"
								prop="Entity.State"
								:rules="[{ required: true, message: '推送结果为必填项', trigger: 'blur' }]"
							>
								<template #label>
									<WtmLabelTooltip label="推送结果" tooltip="短信推送状态" />
								</template>
								<el-select v-model="stateData.vmModel.Entity.State" clearable class="width100">
									<el-option key="创建" value="创建" label="创建"></el-option>
									<el-option key="成功" value="成功" label="成功"></el-option>
									<el-option key="失败" value="失败" label="失败"></el-option
								></el-select>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_Message_FormItem" prop="Entity.Message">
								<template #label>
									<WtmLabelTooltip label="错误消息" tooltip="推送失败时的错误信息" />
								</template>
								<el-input v-model="stateData.vmModel.Entity.Message" clearable class="width100"></el-input>
							</el-form-item>
						</el-col>
						<el-col :xs="24" :lg="12" class="mb20">
							<el-form-item ref="Entity_SmsTime_FormItem" prop="Entity.SmsTime">
								<template #label>
									<WtmLabelTooltip label="短信时间" tooltip="短信推送时间" />
								</template>
								<el-date-picker v-model="stateData.vmModel.Entity.SmsTime" clearable class="width100"></el-date-picker>
							</el-form-item>
						</el-col>
					</el-row>
				</div>

				<div style="text-align: right">
					<WtmButton @click="onSubmit" type="primary" :button-text="$t('message._system.common.vm.submit')" style="margin-top: 15px" />
					<WtmButton @click="onClose" type="default" :button-text="$t('message._system.common.vm.cancel')" style="margin-top: 15px" />
				</div>
			</el-form>
		</el-card>
	</div>
</template>

<script setup lang="ts" name="message._system.common.vm.add;false">
import ElMessage from 'element-plus/es/components/message/index';
import { reactive, ref, getCurrentInstance, onMounted } from 'vue';
import { SysSmsAuthLogApi as api } from '/@/views/_admin/syssmsauthlog/api';
import other from '/@/utils/other';
import { ISysSmsAuthLogVm } from '/@/views/_admin/syssmsauthlog/api/interface';
import { IViewModel } from '/@/types/common';

const ci = getCurrentInstance() as any;
const loading = ref(false);
// 定义变量内容
const formRef = ref();
const stateData = reactive<IViewModel<ISysSmsAuthLogVm>>({
	vmModel: {
		Entity: {
			Phone: null,
			Context: null,
			State: 0,
			Message: null,
			SmsTime: null,
		},
	},
});

// 取消
const onClose = () => {
	closeDialog();
};

// 提交
const onSubmit = () => {
	formRef.value?.validate((valid: boolean) => {
		if (valid) {
			loading.value = true;
			api()
				.add(stateData.vmModel)
				.then(() => {
					ElMessage.success(ci.proxy.$t('message._system.common.vm.submittip'));
					emit('refresh');
					closeDialog();
				})
				.catch((error) => {
					other.setFormError(ci, error);
				})
				.finally(() => {
					loading.value = false;
				});
		}
	});
};

// 页面加载时
onMounted(() => {
	loading.value = true;
	if (ci.attrs['wtmdata']) {
		stateData.vmModel.Entity.ID = ci.attrs['wtmdata'].ID;
	}
	const promises: Promise<any>[] = [];
	Promise.allSettled(promises).finally(() => {
		loading.value = false;
	});
});

// 定义子组件向父组件传值/事件
const emit = defineEmits(['refresh', 'closeDialog']);
// 关闭弹窗
const closeDialog = () => {
	emit('closeDialog');
};
</script>
